package code.whmmm.util.http.pojo;

import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

/**
 * <p><b>
 *
 * </b></p>
 * <br/>
 *
 * @author Zero
 * @date 2019/8/1 14:51
 */
public class TrustSslManager implements X509TrustManager {

    private static final Logger LOGGER = LoggerFactory.getLogger(TrustSslManager.class);

    private static TrustSslManager manager = null;

    private static final SSLConnectionSocketFactory trustSocketFactory;

    static {
        SSLConnectionSocketFactory ssl = null;
        try {
            SSLContext ctx = SSLContext.getInstance("SSL");
            X509TrustManager tm = TrustSslManager.getInstance();
            ctx.init(null, new TrustManager[]{tm}, null);
            ssl = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
        } catch (Exception e) {

            LOGGER.error("TrustSocketFactory 初始化错误!");
        }
        trustSocketFactory = ssl;
    }

    public static X509TrustManager getInstance() {
        if (manager == null) {
            manager = new TrustSslManager();
        }
        return manager;
    }

    public static SSLConnectionSocketFactory getSslFactory() {
       /* SSLConnectionSocketFactory ssf = null;
        try {

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
*/
        return trustSocketFactory;

    }

    @Override
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }

    @Override
    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    }


}
